package com.uwo.house.controller;

import com.uwo.house.beans.User;
import com.uwo.house.beans.request.*;
import com.uwo.house.beans.response.MeasureOrderResp;
import com.uwo.house.beans.response.MessageManagerResp;
import com.uwo.house.config.JWTAuthenticationToken;
import com.uwo.house.kits.CacheKits;
import com.uwo.house.service.*;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by PHS on 2017/7/19.
 * 项目经理中心Controller
 */
@Controller
@RequestMapping("/projectManager")
public class ProjectManagerController extends BaseController{

    @Autowired
    private IProjectManagerService projectManagerService;

    @Autowired
    private IMeasureCenterService measureCenterService;

    @Autowired
    private IDesignCenterService designCenterService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IOrderMeasureService orderMeasureService;

    @Autowired
    private IDecorationSchemeService decorationSchemeService;

    @Autowired
    private IMaterialListService materialListService;

    /**
     * 项目经理端显示客户确认的订单列表
     * @param orderId
     * @param mobile
     * @param projectStatus
     * @return
     */
    @RequestMapping(value = "/projectManagerOrderList", method = RequestMethod.GET)
    @ResponseBody
    public Map projectManagerOrderList(@RequestParam(value = "orderId", required = false) String orderId,
                                       @RequestParam(value = "mobile", required = false) String mobile,
                                       @RequestParam(value = "projectStatus", required = false) Integer projectStatus) {
        List<MeasureCenterReq> projectManagerOrderList = measureCenterService.getMeasureOrderToProjectManager(orderId, mobile, projectStatus);
        for (MeasureCenterReq mc : projectManagerOrderList) {
            String supervisorId = mc.getSupervisorId();
            User user = userService.selectUserById(supervisorId);
            if (null != user) {
                mc.setSupervisorName(user.getUsername());
            }
        }
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, projectManagerOrderList);
    }

    /**
     * 项目经理上传信息接口
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadInfo" , method = RequestMethod.POST)
    public Map uploadInfo(@RequestParam(value = "sname") String sname,
                          @RequestParam(value = "photo") String photo,
                          @RequestParam(value = "video") String video,
                          @RequestParam(value = "introduction") String introduction,
                          @RequestParam(value = "orderId") String orderId,
                          @RequestHeader(value = "Authorization") String token) {
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        User user = jwtToken.getUser();
        String userId = user.getId();
        ProjectManagerReq projectManagerReq = new ProjectManagerReq();
        projectManagerReq.setConstractNumber(orderId);
        projectManagerReq.setIntroduction(introduction);
        projectManagerReq.setPhoto(photo);
        projectManagerReq.setVideo(video);
        projectManagerReq.setStatus(0);
        projectManagerReq.setProjectManagerId(userId);
        projectManagerReq.setSname(sname);
        projectManagerReq.setDelFlag(0);
        int result = projectManagerService.addProjectInfo(projectManagerReq);
        MeasureOrderResp measureOrderResp = orderMeasureService.getMeasureDetailsByOrderId(orderId);
        String yUserId = "";
        if (null != measureOrderResp) {
            yUserId = measureOrderResp.getUserId();
        }
        if (result == 1){
            DecorationScheduleReq decorationScheduleReq = new DecorationScheduleReq();
            decorationScheduleReq.setOrderId(orderId);
            decorationScheduleReq.setActionDesc("项目经理上传了我的新房的施工图片");
            decorationScheduleReq.setActionTime(new Date());
            decorationScheduleReq.setCreateTime(new Date());
            decorationSchemeService.addDecorationSchedule(decorationScheduleReq);
            this.addRecord("项目经理上传了我的新房的施工图片",yUserId,photo,video);
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "上传成功！");
        } else {
            return UtilReturn.resErrorMap("上传失败！",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 项目经理上传施工方案完成后接口
     * @param orderId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/finish" , method = RequestMethod.POST)
    public Map finish(@RequestParam(value = "orderId") String orderId) {
        int result = projectManagerService.updateProjectStatus(orderId);
        if (result == 1){
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "上传成功！");
        } else {
            return UtilReturn.resErrorMap("上传失败！",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 技术资料详情
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/technologyInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map technologyInfo(@RequestParam("orderId") String orderId,
                              @RequestParam(value = "userId") String userId) {
        MeasureCenterReq measureCenterReq = designCenterService.getOrderSpecialInfo(orderId);
        List<DesignOrderStatusReq> list = projectManagerService.findUserByOrderId(orderId);
        List<User> listDesigner = new ArrayList<User>();
        List<User> listSupervisor = new ArrayList<User>();
        for (DesignOrderStatusReq d : list) {
            String designerId = d.getUserId();
            String supervisorId = d.getSupervisorId();
            User designer = userService.selectUserById(designerId);
            User supervisor = userService.selectUserById(supervisorId);
            listDesigner.add(designer);
            listSupervisor.add(supervisor);
        }
        List<MaterialListReq> materialList = materialListService.getMaterialDetailByOrderId(orderId);
        DesignSchemeReq designSchemeReq = projectManagerService.findDesignScheme(orderId,userId);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("measureCenterReq",measureCenterReq);
        map.put("designSchemeReq",designSchemeReq);
        map.put("listDesigner",listDesigner);
        map.put("listSupervisor",listSupervisor);
        map.put("materialList",materialList);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, map);
    }

    /**
     * 显示施工详情接口
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showDetail",method = RequestMethod.POST)
    public Map showDetail(@RequestParam(value = "orderId") String orderId){
        List<ProjectManagerReq> list = projectManagerService.projectDetail(orderId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 项目经理端后期维护订单列表
     * @param orderId
     * @param mobile
     * @param messageType
     * @param handleStatus
     * @return
     */
    @RequestMapping(value = "/projectManagerHouOrderList", method = RequestMethod.POST)
    @ResponseBody
    public Map projectManagerHouOrderList(@RequestParam(value = "orderId", required = false) String orderId,
                                       @RequestParam(value = "mobile", required = false) String mobile,
                                       @RequestParam(value = "messageType", required = false) Integer messageType,
                                       @RequestParam(value = "handleStatus", required = false) Integer handleStatus) {
        List<MessageManagerResp> messageList = projectManagerService.getToProjectManager(orderId,mobile,messageType,handleStatus);
//        for (MeasureCenterReq mc : messageList) {
//            String supervisorId = mc.getSupervisorId();
//            User user = userService.selectUserById(supervisorId);
//            if (null != user) {
//                mc.setSupervisorName(user.getUsername());
//            }
//
//            String supplierId = mc.getSupplierId();
//            User user1 = userService.selectUserById(supplierId);
//            if (null != user1) {
//                mc.setSupplierName(user1.getUsername());
//            }
//        }
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, messageList);
    }

    /**
     * 项目经理回复用户页面接口
     * @return
     */
    @RequestMapping(value = "/toManagerReplyPage", method = RequestMethod.POST)
    @ResponseBody
    public Map toManagerReplyPage(@RequestParam(value = "orderId") String orderId,
                                  @RequestParam(value = "messageId") Integer messageId){
        // 根据合同编号 与 消息id
        MeasureCenterReq measureCenterReq = projectManagerService.findMessageInfo(orderId,messageId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, measureCenterReq);
    }

    /**
     * 项目经理回复用户接口
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/managerReply", method = RequestMethod.POST)
    public Map managerReply(@RequestParam(value = "orderId") String orderId,
                              @RequestParam(value = "replay") String replay,
                              @RequestParam(value = "photo") String photo,
                              @RequestParam(value = "messageId") Integer messageId,
                              @RequestHeader(value = "Authorization") String token){
        ProjectManagerReplyReq projectManagerReplyReq = new ProjectManagerReplyReq();
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        User user = jwtToken.getUser();
        String userId = user.getId();
        projectManagerReplyReq.setReplay(replay);
        projectManagerReplyReq.setContractNumber(orderId);
        projectManagerReplyReq.setPhoto(photo);
        projectManagerReplyReq.setManagerId(userId);
        projectManagerReplyReq.setMessageId(messageId);
        int result = projectManagerService.addReplay(projectManagerReplyReq);
        if (result == 1){
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "回复成功！");
        } else {
            return UtilReturn.resErrorMap("回复失败！",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 项目经理端显示回复详情信息
     * @return
     */
    @RequestMapping(value = "/findMessageDetail", method = RequestMethod.POST)
    @ResponseBody
    public Map findMessageDetail(@RequestParam(value = "orderId") String orderId,
                                 @RequestParam(value = "messageId") Integer messageId){
        // 根据合同编号 与 消息id
        MeasureCenterReq measureCenterReq = projectManagerService.findMessageDetail(orderId,messageId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, measureCenterReq);
    }

}
